package com.gylang.leave.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gylang.common.utils.DateUtils;
import com.gylang.common.utils.SecurityUtils;
import com.gylang.leave.domain.WorkflowLeave;
import com.gylang.leave.mapper.WorkflowLeaveMapper;
import com.gylang.leave.service.IWorkflowLeaveService;
import com.gylang.system.service.ISysUserService;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 请假Service业务层处理
 *
 * @author danny
 * @date 2020-10-28
 */
@Service
public class WorkflowLeaveServiceImpl extends ServiceImpl<WorkflowLeaveMapper,WorkflowLeave> implements IWorkflowLeaveService {

	@Autowired
	private WorkflowLeaveMapper workflowLeaveMapper;
	@Autowired
	private ProcessRuntime processRuntime;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private TaskService taskService;


	/**
	 * 查询请假
	 *
	 * @param id 请假ID
	 * @return 请假
	 */
	@Override
	public WorkflowLeave selectWorkflowLeaveById(String id) {
		return workflowLeaveMapper.selectWorkflowLeaveById(id);
	}

	/**
	 * 查询请假列表
	 *
	 * @param workflowLeave 请假
	 * @param objectPage
	 * @return 请假
	 */
	@Override
	public Page<WorkflowLeave> selectWorkflowLeaveList(WorkflowLeave workflowLeave, Page<WorkflowLeave> objectPage) {

		return workflowLeaveMapper.selectWorkflowLeaveListByWorkflowLeaveAndDeptId(objectPage,workflowLeave, SecurityUtils.getLoginUser().getUser().getDeptId());
	}

	/**
	 * 查询请假列表带任务状态
	 *
	 * @param workflowLeave 请假
	 * @param objectPage
	 * @return 请假
	 */
	@Override
	public Page<WorkflowLeave> selectWorkflowLeaveAndTaskNameList(WorkflowLeave workflowLeave, Page<WorkflowLeave> objectPage) {

		Page<WorkflowLeave> workflowLeaves = page(objectPage, new QueryWrapper<>(workflowLeave));

		List<String> collect = workflowLeaves.getRecords().parallelStream().map(wl -> wl.getInstanceId()).collect(Collectors.toList());
		if (collect != null && !collect.isEmpty()) {
			List<Task> tasks = taskService.createTaskQuery().processInstanceIdIn(collect).list();
			workflowLeaves.getRecords().forEach(
				wl -> {
					Task task = tasks.parallelStream().filter(t -> t.getProcessInstanceId().equals(wl.getInstanceId())).findAny().orElse(null);
					if (task != null) {
						wl.setTaskName(task.getName());
					}
				}
			);
		}
		return workflowLeaves;
	}

	/**
	 * 新增请假
	 *
	 * @param workflowLeave 请假
	 * @return 结果
	 */
	@Override
	public int insertWorkflowLeave(WorkflowLeave workflowLeave) {

		String id = UUID.randomUUID().toString();
		workflowLeave.setId(id);
		workflowLeave.setCreateTime(DateUtils.getNowDate());
		String join = StrUtil.join(",", sysUserService.selectUserNameByPostCodeAndDeptId("se", SecurityUtils.getLoginUser().getUser().getDeptId()));
		ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder
			.start()
			.withProcessDefinitionKey("leave")
			.withName(workflowLeave.getTitle())
			.withBusinessKey(id)
			.withVariable("deptLeader", join)
			.build());
		workflowLeave.setInstanceId(processInstance.getId());
		workflowLeave.setState("0");
		workflowLeave.setCreateName(SecurityUtils.getUsername());
		workflowLeave.setCreateBy(SecurityUtils.getUsername());
		workflowLeave.setCreateTime(DateUtils.getNowDate());
		return workflowLeaveMapper.insertWorkflowLeave(workflowLeave);
	}

	/**
	 * 修改请假
	 *
	 * @param workflowLeave 请假
	 * @return 结果
	 */
	@Override
	public int updateWorkflowLeave(WorkflowLeave workflowLeave) {
		workflowLeave.setUpdateTime(DateUtils.getNowDate());
		return workflowLeaveMapper.updateWorkflowLeave(workflowLeave);
	}

	/**
	 * 批量删除请假
	 *
	 * @param ids 需要删除的请假ID
	 * @return 结果
	 */
	@Override
	public int deleteWorkflowLeaveByIds(String[] ids) {
		return workflowLeaveMapper.deleteWorkflowLeaveByIds(ids);
	}

	/**
	 * 删除请假信息
	 *
	 * @param id 请假ID
	 * @return 结果
	 */
	@Override
	public int deleteWorkflowLeaveById(String id) {
		return workflowLeaveMapper.deleteWorkflowLeaveById(id);
	}

	@Override
	public WorkflowLeave selectWorkflowLeaveByInstanceId(String instanceId) {

		return workflowLeaveMapper.selectWorkflowLeaveByInstanceId(instanceId);
	}
}
